/*
 * AY 2014/15 Semester 1 IS2103 Assignment Part2
 * Matric. No.:    A0102330H
 * Name:           Cui Lin
 */
package ejb;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author cuilin
 */
@MessageDriven(mappedName = "jms/Topic", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
    @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
    @ActivationConfigProperty(propertyName = "clientId", propertyValue = "AppealMessageDrivenBean"),
    @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "AppealMessageDrivenBean")
})
public class AppealMessageDrivenBean implements MessageListener {

    @PersistenceContext
    EntityManager em;
    private AppealEntity appealEntity;

    public AppealMessageDrivenBean() {
    }

    private void createAppeal(String matricNum, String content) {
        String t = getCurrentTime();
        String status = "unread";
        String comment = "";

        System.out.println("AppealMessageDrivenBean createAppeal: " + matricNum + "  " + content + t);
        appealEntity = new AppealEntity();
        appealEntity.create(t, content, status, comment);

        StudentEntity student = findStudent(matricNum);

        if (null != student) {

            System.out.println("AppealMessageDrivenBean createAppeal student " + student.getId());
            //appealEntity.setStudent(student);
            student.addAppealEntity(appealEntity);
            em.persist(student);
            em.flush();

            System.out.println("AppealMessageDrivenBean success create appeal for " + matricNum + "  " + content);
        } else {
            System.out.println("AppealMessageDrivenBean createAppeal student not found");
        }
    }

    private StudentEntity findStudent(String matricNumber) {
        Query q = em.createQuery("SELECT s FROM StudentEntity s WHERE s.matricNumber='" + matricNumber + "'");
        List students = q.getResultList();
        if (null == students || students.isEmpty()) {
            return null;
        }

        return (StudentEntity) students.get(0);
    }

    private String getCurrentTime() {
        int day, month, year;
        int second, minute, hour;
        GregorianCalendar date = new GregorianCalendar();

        day = date.get(Calendar.DAY_OF_MONTH);
        month = date.get(Calendar.MONTH);
        year = date.get(Calendar.YEAR);

        second = date.get(Calendar.SECOND);
        minute = date.get(Calendar.MINUTE);
        hour = date.get(Calendar.HOUR);

        String t = day + "/" + month + "/" + year + " " + hour + ":" + minute + ":" + second;
        return t;
    }

    @Override
    public void onMessage(Message inMessage) {
        MapMessage msg;
        try {
            if (inMessage instanceof MapMessage) {
                msg = (MapMessage) inMessage;
                String matricNum = msg.getString("matricNum");
                String content = msg.getString("content");

                System.out.println("AppealMessageDrivenBean onMessage: " + matricNum + "  " + content);

                createAppeal(matricNum, content);

            } else {
                System.out.println("AppealMessageDrivenBean: onMessage(): Message of wrong type: " + inMessage.getClass().getName());
            }
        } catch (Exception ex) {
            System.out.println("AppealMessageDrivenBean: onMessage(): Exception: " + ex.toString());
        }
    }
}
